//
//  BallButton.m
//  K8彩票
//
//  Created by zyn on 2017/6/8.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "BallButton.h"
#import "Config.h"

@implementation BallButton
@synthesize callback;

- (instancetype)initWithNumber:(NSInteger) number type:(NSInteger )type{
    self = [super init];
    if (self) {
        int width = (ScreenWidth()  - 8 * 12) / 7;
        int offset = number % 7 == 0 ? 6 * width + 7 * 12 : (number % 7  -  1)* width + (number % 7) * 12;
        long leading = number < 8 ? (number - 1) * width + 12 * number : offset;
        long toping = ((number - 1) / 7 + 1) * 12 + (number - 1) / 7 * width;
        
        self.frame = CGRectMake(leading, toping,width, width);
        [self setTitleColor:type == RED_BALL ? [UIColor redColor] : [UIColor blueColor] forState:UIControlStateNormal];
        self.backgroundColor = [UIColor whiteColor];
        if (number < 10)
            [self setTitle:[NSString stringWithFormat:@"0%ld",(long)number] forState:UIControlStateNormal];
        else
            [self setTitle:[NSString stringWithFormat:@"%ld",(long)number] forState:UIControlStateNormal];
        
        self.layer.cornerRadius = width / 2;
        self.layer.borderColor = [[UIColor grayColor]CGColor];
        self.layer.borderWidth = .5;
        
        self.tag = number;
        [self addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        _isSelect = NO;
        typeball = type;
    }
    return self;
    
}

-(instancetype)initWithNumber:(NSInteger)number superView:(UIView *)superView{
    self = [super init];
    if (self) {
        int width = (ScreenWidth()  -  40 - 48 - 6 * 12) / 5;
        int offset = number % 5 == 0 ? 4 * width + 5 * 12 : (number % 5  -  1)* width + (number % 5) * 12;
        long leading = number < 6 ? (number - 1) * width + 12 * number : offset;
        long toping = ((number - 1) / 5 + 1) * 12 + (number - 1) / 5 * width;
        [superView addSubview:self];
        
        self.frame = CGRectMake(leading, toping,width, width);
        [self setTitleColor: [UIColor redColor] forState:UIControlStateNormal];
        self.backgroundColor = [UIColor whiteColor];
        [self setTitle:[NSString stringWithFormat:@"%ld",(long)number - 1] forState:UIControlStateNormal];
        
        self.layer.cornerRadius = width / 2;
        self.layer.borderColor = [[UIColor grayColor]CGColor];
        self.layer.borderWidth = .5;
        
        self.tag = number - 1;
        [self addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        _isSelect = NO;
    }
    return self;
}

-(void)onClick:(UIButton *)btn{
    [self translationAnimation];
    if (_isSelect){
        self.backgroundColor = [UIColor whiteColor];
        [self setTitleColor:typeball == RED_BALL ? [UIColor redColor] : [UIColor blueColor] forState:UIControlStateNormal];
    }else{
        self.backgroundColor = typeball == RED_BALL ? [UIColor redColor] : [UIColor blueColor];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    _isSelect = !_isSelect;
    [callback onClickBall];
    
}


-(void)translationAnimation{
    
    CAKeyframeAnimation *orbit = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    //2.设置关键帧,这里有四个关键帧
    CATransform3D scale1 = CATransform3DMakeScale(0.0, 0.0, 1);
    CATransform3D scale2 = CATransform3DMakeScale(1.1, 1.1, 1);
    CATransform3D scale3 = CATransform3DMakeScale(0.9, 0.9, 1);
    CATransform3D scale4 = CATransform3DMakeScale(1.0, 1.0, 1);
    
    NSValue *key1=[NSValue valueWithCATransform3D:scale1];//对于关键帧动画初始值不能省略
    NSValue *key2=[NSValue valueWithCATransform3D:scale2];
    NSValue *key3=[NSValue valueWithCATransform3D:scale3];
    NSValue *key4=[NSValue valueWithCATransform3D:scale4];
    NSArray *values=@[key1,key2,key3,key4];
    
    orbit.duration = 0.4;
    orbit.values = values;
    orbit.keyTimes = @[@0.0f,@0.6f,@0.8f,@1.0f];
    
    orbit.beginTime = CACurrentMediaTime();
    [self.layer addAnimation:orbit forKey:@"orbit"];
}

@end
